package com.orange.score.activity.entry;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TabActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RemoteViews;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.Toast;
import com.mobclick.android.MobclickAgent;
import com.orange.common.android.widget.RealtimeScorePopupWindow;
import com.orange.score.android.service.FollowMatchService;
import com.orange.score.android.service.ScoreService;
import com.orange.score.android.service.ScoreUpdateService;
import com.orange.score.app.ScoreApplication;
import com.orange.score.model.config.ConfigManager;
import com.orange.score.model.user.UserManager;
import com.orange.score.service.MatchService;

public class MainActivity extends TabActivity
  implements RadioGroup.OnCheckedChangeListener
{
  public static final String MORE_TAG = "more";
  public static final String REALTIME_INDEX_TAG = "realtime_index";
  public static final String REALTIME_MATCH_TAG = "realtime_match";
  public static final String REPOSITORY_TAG = "repository";
  public static final String SCORE_UPDATE_TAG = "score_update";
  public static final String TAG = "MainActivity";
  static String currentTag;
  private static boolean showNetError = false;
  public static Notification statusNotification = null;
  public PowerManager.WakeLock mWakeLock;
  BroadcastReceiver networkStateReceiver = null;
  private RadioGroup radioderGroup;
  RealtimeScorePopupWindow realtimeScorePopupWindow = new RealtimeScorePopupWindow();
  private TabHost tabHost;

  static
  {
    currentTag = "realtime_match";
  }

  private void addNotificaction()
  {
    if (statusNotification != null);
    while (true)
    {
      return;
      NotificationManager localNotificationManager = (NotificationManager)getSystemService("notification");
      statusNotification = new Notification(2130837635, "球探体育比分", System.currentTimeMillis());
      statusNotification.flags = 34;
      Intent localIntent = new Intent(getApplicationContext(), MainActivity.class);
      localIntent.setFlags(4194304);
      localIntent.setAction("android.intent.action.MAIN");
      localIntent.addCategory("android.intent.category.LAUNCHER");
      PendingIntent localPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, localIntent, 134217728);
      RemoteViews localRemoteViews = new RemoteViews(getPackageName(), 2130903040);
      localRemoteViews.setImageViewResource(2131558401, 2130837635);
      localRemoteViews.setTextViewText(2131558402, "球探体育比分");
      statusNotification.contentView = localRemoteViews;
      statusNotification.contentIntent = localPendingIntent;
      localNotificationManager.notify(2130837635, statusNotification);
    }
  }

  private TabHost.TabSpec buildTabSpec(String paramString1, String paramString2, Intent paramIntent)
  {
    return this.tabHost.newTabSpec(paramString1).setIndicator(paramString2).setContent(paramIntent);
  }

  private void registerNetworkReceiver()
  {
    this.networkStateReceiver = new BroadcastReceiver(this)
    {
      public void onReceive(, Intent paramIntent)
      {
        if ((!(this.this$0.haveInternet(this.this$0.getApplicationContext()))) && (!(MainActivity.access$0())))
        {
          MainActivity.access$1(true);
          Toast.makeText(this.this$0.getApplicationContext(), "网络连接错误，请检查网络是否连接", 1).show();
          Log.w("MainActivity", "Network Type not connected");
        }
        while (true)
        {
          return;
          MainActivity.access$1(false);
          Log.d("MainActivity", "Network Type is connected");
        }
      }
    };
    IntentFilter localIntentFilter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
    registerReceiver(this.networkStateReceiver, localIntentFilter);
  }

  private void unregisterNetworkReceiver()
  {
    if (this.networkStateReceiver != null)
    {
      unregisterReceiver(this.networkStateReceiver);
      this.networkStateReceiver = null;
    }
  }

  public boolean haveInternet(Context paramContext)
  {
    int i;
    NetworkInfo localNetworkInfo = ((ConnectivityManager)paramContext.getSystemService("connectivity")).getActiveNetworkInfo();
    if ((localNetworkInfo == null) || (!(localNetworkInfo.isConnected())))
      i = 0;
    while (true)
    {
      while (true)
      {
        return i;
        if (!(localNetworkInfo.isRoaming()))
          break;
        i = 0;
      }
      i = 1;
    }
  }

  public void onCheckedChanged(RadioGroup paramRadioGroup, int paramInt)
  {
    switch (paramInt)
    {
    default:
    case 2131558458:
    case 2131558459:
    case 2131558460:
    case 2131558461:
    case 2131558462:
    }
    while (true)
    {
      while (true)
      {
        while (true)
        {
          while (true)
          {
            while (true)
            {
              return;
              this.tabHost.setCurrentTabByTag("score_update");
              currentTag = "score_update";
            }
            this.tabHost.setCurrentTabByTag("realtime_match");
            currentTag = "realtime_match";
          }
          this.tabHost.setCurrentTabByTag("realtime_index");
          currentTag = "realtime_index";
        }
        this.tabHost.setCurrentTabByTag("repository");
        currentTag = "repository";
      }
      this.tabHost.setCurrentTabByTag("more");
      currentTag = "more";
    }
  }

  public void onCreate(Bundle paramBundle)
  {
    ScoreApplication.setMainActivity(this);
    ScoreApplication.setAlive();
    ScoreApplication.setBackground(false);
    SharedPreferences localSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    ConfigManager.getInstance().setSharePreference(localSharedPreferences);
    UserManager.getInstance().setSharePreference(localSharedPreferences);
    startService(new Intent(this, ScoreService.class));
    startService(new Intent(this, FollowMatchService.class));
    startService(new Intent(this, ScoreUpdateService.class));
    super.onCreate(paramBundle);
    UserManager.getInstance().checkRegister();
    requestWindowFeature(1);
    setContentView(2130903070);
    this.tabHost = getTabHost();
    this.tabHost.addTab(buildTabSpec("score_update", "score_update", new Intent(this, ScoreUpdateActivity.class)));
    this.tabHost.addTab(buildTabSpec("realtime_match", "realtime_match", new Intent(this, RealtimeMatchActivity.class)));
    this.tabHost.addTab(buildTabSpec("realtime_index", "realtime_index", new Intent(this, RealtimeIndexActivity2.class)));
    this.tabHost.addTab(buildTabSpec("repository", "repository", new Intent(this, RepositoryActivity.class)));
    this.tabHost.addTab(buildTabSpec("more", "more", new Intent(this, MoreActivity.class)));
    this.radioderGroup = ((RadioGroup)findViewById(2131558457));
    this.radioderGroup.setOnCheckedChangeListener(this);
    selectTab(currentTag);
    addNotificaction();
    registerNetworkReceiver();
    ScoreApplication.realtimeMatchService.addScoreLiveUpdateObserverAsFirst(this.realtimeScorePopupWindow);
    this.mWakeLock = ((PowerManager)getSystemService("power")).newWakeLock(6, "My Tag");
  }

  public void onDestroy()
  {
    Log.d("MainActivity", "onDestroy");
    unregisterNetworkReceiver();
    ScoreApplication.realtimeMatchService.removeScoreLiveUpdateObserver(this.realtimeScorePopupWindow);
    RealtimeScorePopupWindow.cancelDismissPopupWindowTimer();
    ScoreApplication.setBackground(true);
    super.onDestroy();
  }

  public void onPause()
  {
    super.onPause();
    ScoreApplication.setBackground(true);
    MobclickAgent.setDebugMode(false);
    MobclickAgent.onPause(this);
    if ((ConfigManager.getInstance().isNotAutoLock()) && (this.mWakeLock.isHeld()))
      this.mWakeLock.release();
  }

  public void onRestart()
  {
    super.onRestart();
    if (!(haveInternet(getApplicationContext())))
    {
      Toast.makeText(getApplicationContext(), "网络连接错误，请检查网络是否连接", 1).show();
      Log.w("MainActivity", "<onRestart>Network Type not connected");
    }
  }

  public void onResume()
  {
    super.onResume();
    ScoreApplication.setBackground(false);
    MobclickAgent.setDebugMode(false);
    MobclickAgent.onResume(this);
    if ((ConfigManager.getInstance().isNotAutoLock()) && (!(this.mWakeLock.isHeld())))
      this.mWakeLock.acquire();
  }

  public void selectTab(String paramString)
  {
    if (paramString == null);
    while (true)
    {
      do
        while (true)
        {
          while (true)
          {
            while (true)
            {
              while (true)
              {
                return;
                if (!(paramString.equals("score_update")))
                  break;
                this.radioderGroup.check(2131558458);
              }
              if (!(paramString.equals("realtime_match")))
                break;
              this.radioderGroup.check(2131558459);
            }
            if (!(paramString.equals("realtime_index")))
              break;
            this.radioderGroup.check(2131558460);
          }
          if (!(paramString.equals("repository")))
            break;
          this.radioderGroup.check(2131558461);
        }
      while (!(paramString.equals("more")));
      this.radioderGroup.check(2131558462);
    }
  }
}